#include "cJSON/cJSON.h"
#include "log/log.h"
#include <stdlib.h>

int main()
{
    // 1.生成cJSON
    char *name ="张扬";
    int age = 22;
    cJSON *root = cJSON_CreateObject();
    if (root == NULL)
    {
        log_error("cJSON_CreateObject failed");
        return -1;
    }
    cJSON_AddStringToObject(root,"name",name);
    cJSON_AddNumberToObject(root,"age",age);
    
    char *json=cJSON_PrintUnformatted(root);
    log_debug("生成的json:%s",json);

    // 2.解析cJSON
    cJSON *parse_root=cJSON_Parse(json);
    if (parse_root == NULL)
    {
        log_error("cJSON_Parse failed");
        return -1;
    }
    cJSON *nameobj= cJSON_GetObjectItem(parse_root,"name");
    cJSON *ageobj= cJSON_GetObjectItem(parse_root,"age");
    char *str_name = nameobj->valuestring;
    int age_int = ageobj->valueint;
    log_debug("解析json: name = %s, age = %d", str_name, age_int);

    // 3. 释放内存
    cJSON_Delete(root);
    cJSON_Delete(parse_root);
    free(json);
    return 0;
}